package org.ssp.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ssp.pojo.Mail;
import org.ssp.test.MailUtil;

import net.sf.json.JSONObject;

/**
 * 咨询处理
 * 
 */
@WebServlet("/consult")
public class ConsultServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method=request.getParameter("method");
		/*if(request.getSession().getAttribute("assist")==null){
			request.getRequestDispatcher("/WEB-INF/page/admin/admin-login.jsp").forward(request, response);
		}*/
	/*	if("study".equals(method)){
			request.getRequestDispatcher("/WEB-INF/page/user/studyconsult.jsp").forward(request, response);;
		}else if("rent".equals(method)){
			request.getRequestDispatcher("/WEB-INF/page/user/rentconsult.jsp").forward(request, response);;

		}else if("travel".equals(method)){
			request.getRequestDispatcher("/WEB-INF/page/user/travelconsult.jsp").forward(request, response);;
		}else*/ 
		if("email".equals(method)){//发送邮件提交留言咨询,设置不同的账户
			String to=request.getParameter("to");
			if("1".equals(to)){//发给留学咨询邮箱
				String phone=request.getParameter("phone");
				String email=request.getParameter("email");
				String content=request.getParameter("content");
				//邮件内容以表格的形式发送
				String contenttab="<table border='1' style='border-collapse:collapse'>"
						+ "<tr><td>电话</td><td>"+phone+"</td></tr>"
						+ "<tr><td>邮箱</td><td>"+email+"</td></tr>"
						+ "<tr><td>问题</td><td>"+content+"</td></tr>"
						+ "</table>";
				//创建mail对象
				Mail mail=new Mail();
				mail.setHost("smtp.qq.com");
				mail.setUser("1255292979");
				mail.setPass("slhphqvgsujkjbgi");
				mail.setFrom("1255292979@qq.com");
				mail.setTo(new String[]{"1255292979@qq.com","friends_su@163.com"});
				mail.setSubject("留学申请咨询");
				mail.setContent(contenttab);
				Session session=MailUtil.getSession1();
				Transport ts=null;
				try {
					ts=session.getTransport();
					Message message=MailUtil.createMail(session, mail);
					ts.connect(mail.getHost(),mail.getUser(),mail.getPass());
					ts.sendMessage(message, message.getAllRecipients());
				} catch (NoSuchProviderException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					try {
						ts.close();
					} catch (MessagingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}else if("2".equals(to)){//发给房屋租赁咨询邮箱
				String phone=request.getParameter("phone");
				String email=request.getParameter("email");
				String content=request.getParameter("content");
				//邮件内容以表格的形式发送
				String contenttab="<table border='1' style='border-collapse:collapse'>"
						+ "<tr><td>电话</td><td>"+phone+"</td></tr>"
						+ "<tr><td>邮箱</td><td>"+email+"</td></tr>"
						+ "<tr><td>问题</td><td>"+content+"</td></tr>"
						+ "</table>";
				//创建mail对象
				Mail mail=new Mail();
				mail.setHost("smtp.qq.com");
				mail.setUser("1255292979");
				mail.setPass("slhphqvgsujkjbgi");
				mail.setFrom("1255292979@qq.com");
				mail.setTo(new String[]{"1255292979@qq.com","friends_su@163.com"});
				mail.setSubject("留学申请咨询");
				mail.setContent(contenttab);
				Session session=MailUtil.getSession1();
				Transport ts=null;
				try {
					ts=session.getTransport();
					Message message=MailUtil.createMail(session, mail);
					ts.connect(mail.getHost(),mail.getUser(),mail.getPass());
					ts.sendMessage(message, message.getAllRecipients());
				} catch (NoSuchProviderException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					try {
						ts.close();
					} catch (MessagingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}else if("3".equals(to)){//发给旅游咨询邮箱
				String phone=request.getParameter("phone");
				String email=request.getParameter("email");
				String content=request.getParameter("content");
				//邮件内容以表格的形式发送
				String contenttab="<table border='1' style='border-collapse:collapse'>"
						+ "<tr><td>电话</td><td>"+phone+"</td></tr>"
						+ "<tr><td>邮箱</td><td>"+email+"</td></tr>"
						+ "<tr><td>问题</td><td>"+content+"</td></tr>"
						+ "</table>";
				//创建mail对象
				Mail mail=new Mail();
				mail.setHost("smtp.qq.com");
				mail.setUser("1255292979");
				mail.setPass("slhphqvgsujkjbgi");
				mail.setFrom("1255292979@qq.com");
				mail.setTo(new String[]{"1255292979@qq.com","friends_su@163.com"});
				mail.setSubject("留学申请咨询");
				mail.setContent(contenttab);
				Session session=MailUtil.getSession1();
				Transport ts=null;
				try {
					ts=session.getTransport();
					Message message=MailUtil.createMail(session, mail);
					ts.connect(mail.getHost(),mail.getUser(),mail.getPass());
					ts.sendMessage(message, message.getAllRecipients());
				} catch (NoSuchProviderException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					try {
						ts.close();
					} catch (MessagingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			JSONObject json=new JSONObject();
			json.element("ok", true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
